package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Inputbill;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.ColorUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SelectedValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalStatistidAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private Object object;
    private int dataType;
    private Context mContext;

    public CoalStatistidAdapter(Object obj, Context context, int dataType) {
        this.dataType = dataType;
        this.mContext = context;
        this.object = obj;
    }

    @Override
    public int getItemViewType(int position) {
        if (object != null && object instanceof List) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_statistic_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder headerViewHolder = (ViewHolder) holder;
            headerViewHolder.BindViewData(object);
            headerViewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        return 1;

    }

    public int getItemColumnSpan(int position) {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView nestBtn;
        PieChartView pieChartView;
        TextView inWeight;
        TextView outWeight;
        TextView differWeight;
        private int lastPosition = -1;
        private int basePosition;
        private List<SliceValue> sliceValueList;

        public ViewHolder(View itemView) {
            super(itemView);
            nestBtn = (CircleImageView) itemView.findViewById(R.id.next_btn);
            pieChartView = (PieChartView) itemView.findViewById(R.id.pieChartView);
            inWeight = (TextView) itemView.findViewById(R.id.in_weight);
            outWeight = (TextView) itemView.findViewById(R.id.out_weight);
            differWeight = (TextView) itemView.findViewById(R.id.differ_weight);
        }

        public void BindViewData(final Object object) {
            String subText = "对比图";
            String centerText = "";
            SliceValue sliceValue;
            List<Inputbill> inputbills = null;
            List<OutputBill> outputBills = null;
            sliceValueList = new ArrayList<>();
            switch (dataType) {
                case 0:
                    centerText = "客户供应";
                    inputbills = (List<Inputbill>) object;
                    break;
                case 1:
                    centerText = "客户采购";
                    outputBills = (List<OutputBill>) object;
                    break;
                case 2:
                    centerText = "煤种采购";
                    inputbills = (List<Inputbill>) object;
                    break;
                case 3:
                    centerText = "煤种供应";
                    outputBills = (List<OutputBill>) object;
                    break;
            }
            if (inputbills != null && !inputbills.isEmpty()) {
                for (int i = 0; i < inputbills.size(); i++) {
                    sliceValue = new SliceValue();
                    sliceValue.setValue(inputbills.get(i).getInputnetweight());
                    if (dataType == 0) {
                        sliceValue.setLabel(inputbills.get(i).getSupplycompany() + " " +
                                inputbills.get(i).getInputnetweight() + " 吨");
                        sliceValue.setColor(Color.parseColor(ColorUtils.getRandColorCode()));
                    } else {
                        sliceValue.setLabel(inputbills.get(i).getCoaltype() + " " +
                                inputbills.get(i).getInputnetweight() + " 吨");
                        sliceValue.setColor(Color.parseColor(ColorUtils.getRandColorCode()));
                    }
                    sliceValueList.add(sliceValue);
                }
            } else if (outputBills != null && !outputBills.isEmpty()) {
                for (int i = 0; i < outputBills.size(); i++) {
                    sliceValue = new SliceValue();
                    sliceValue.setValue(outputBills.get(i).getSendnetweight());
                    if (dataType == 1) {
                        sliceValue.setLabel(outputBills.get(i).getBuycompany() + " " +
                                outputBills.get(i).getSendnetweight() + " 吨");
                        sliceValue.setColor(Color.parseColor(ColorUtils.getRandColorCode()));
                    } else {
                        sliceValue.setLabel(outputBills.get(i).getCoaltype() + " " +
                                outputBills.get(i).getSendnetweight() + " 吨");
                        sliceValue.setColor(Color.parseColor(ColorUtils.getRandColorCode()));
                    }
                    sliceValueList.add(sliceValue);
                }
            }

            PieChartData data = new PieChartData();
            data.setValues(sliceValueList);
            data.setHasCenterCircle(true);
            data.setCenterText1(centerText);
            data.setCenterText2(subText);
            data.setCenterText2Color(mContext.getResources().getColor(R.color.viewfinder_mask));
            data.setCenterCircleColor(mContext.getResources().getColor(R.color.write));

            pieChartView.setPieChartData(data);
            pieChartView.setOnValueTouchListener(getValueSelectListener());
            pieChartView.setCircleFillRatio(1);
            pieChartView.setChartRotation(45, false);
            pieChartView.setChartRotationEnabled(true);
        }

        public void setViewListener(final int position) {
            nestBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (lastPosition != -1) {
                        basePosition = lastPosition;
                    }
                    basePosition = (basePosition - 1 + sliceValueList.size()) % sliceValueList.size();
                    SelectedValue selectedValue =
                            new SelectedValue(basePosition, 0, SelectedValue.SelectedValueType.NONE);
                    pieChartView.selectValue(selectedValue);
                }
            });
        }
    }


    public PieChartOnValueSelectListener getValueSelectListener() {
        PieChartOnValueSelectListener listener = new PieChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int arcIndex, final SliceValue value) {
                Snackbar snackbar = Snackbar
                        .with(mContext)
                        .type(SnackbarType.MULTI_LINE)
                        .duration(Snackbar.SnackbarDuration.LENGTH_SHORT)
                        .position(Snackbar.SnackbarPosition.BOTTOM)
                        .margin(15, 15)
                        .backgroundDrawable(R.color.colorPrimary)
                        .text(String.valueOf(value.getLabel()))
                        .textColor(Color.WHITE)
                        .actionLabel("查看")
                        .actionColor(Color.WHITE)
                        .actionListener(new ActionClickListener() {
                            @Override
                            public void onActionClicked(Snackbar snackbar) {
                                ToastUtils.showShort(value.getLabel().toString());
                            }
                        });
                SnackbarManager.show(snackbar);
            }

            @Override
            public void onValueDeselected() {

            }
        };
        return listener;
    }
}
